<?php
/**
 * @package Freshine
 * @subpackage yStore
 * @since yStore 1.0.0
 */
/*-----------------------------------------------------------------------------------*/
/*	Add Theme Default Options
/*-----------------------------------------------------------------------------------*/
add_action('after_setup_theme', 'ystore_default_options');
function ystore_default_options() {
	if ( is_admin() && isset($_GET['activated'] )) {
		update_option( 'f9_logo_width', '160');
		update_option( 'f9_logo_height', '60');
		update_option( 'f9_site_layout', 1);
		update_option( 'f9_sidebar_top', true);
		update_option( 'f9_sidebar_content_top', true);
		update_option( 'f9_sidebar_content_bottom', true);
		update_option( 'f9_hide_wp_version', true);
		update_option( 'medium_size_w', '570');
		update_option( 'medium_size_h', '270');
		update_option( 'f9_slide_width', '940');
		update_option( 'f9_slide_height', '360');
		update_option( 'f9_slide_timeout', '36000');
		
		//For JigoShop
		update_option( 'jigoshop_catalog_columns', '3');
		update_option( 'jigoshop_catalog_per_page', '6');
		update_option( 'jigoshop_shop_small_w', '210');
		update_option( 'jigoshop_shop_small_h', '210');
		update_option( 'jigoshop_shop_thumbnail_w', '130');
		update_option( 'jigoshop_shop_thumbnail_h', '130');
	}
}

/*-----------------------------------------------------------------------------------*/
/*	yStore Script
/*-----------------------------------------------------------------------------------*/
function ystore_jcarousel_script() { ?>
	<script src="<?php echo get_bloginfo('stylesheet_directory') . '/js/jquery.jcarousel.min.js'; ?>" type="text/javascript"></script>
	<script type="text/javascript">
	//jCarousel
    jQuery('#jcarousel').jcarousel({
		scroll: 1,
		wrap: 'circular',
	});
	</script>
<?php }
add_action( 'wp_footer', 'ystore_jcarousel_script' );

/*-----------------------------------------------------------------------------------*/
/*	yStore Blog Query
/*-----------------------------------------------------------------------------------*/
class yStore_Query_Widget extends WP_Widget {
	/** constructor */
	function __construct() {
		parent::WP_Widget( /* Base ID */'ystore_query', /* Name */'yStore: Blog Query', array( 'description' => 'Query posts from your blog' ) );
	}

	/** @see WP_Widget::widget */
	function widget( $args, $instance ) {
		extract( $args );
		$title = apply_filters( 'widget_title', $instance['title'] );
		$cat_id = $instance['cat_id'];
		$num_post = $instance['num_post'];
		$num_post_with_content = $instance['num_post_with_content'];
		$content_excerpt = $instance['content_excerpt'];
		
		echo $before_widget;
		if ( $title ) echo $before_title . $title . $after_title;
			$the_query = new WP_Query( 'posts_per_page='.$num_post.'&cat='.$cat_id);
			if($the_query->have_posts()) :
				$post_count = 0;
				while ( $the_query->have_posts() ) : $the_query->the_post(); $post_count++;
				if($post_count<=$num_post_with_content) :
				?>
				
				<div id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?>>
					<div class="date">
						<?php echo get_the_date(); ?>
					</div>
					<h6><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'f9vn' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h6>
					<?php the_content(); ?>
				</div><!-- #post-<?php the_ID(); ?> -->
				<?php
				if($post_count==$num_post_with_content) { echo '<ul class="more-posts">'; };
				else : ?>
				<li><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'f9vn' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></li>
				<?php endif;
				endwhile;
				echo "</ul>";
			else :
				echo "<p>This category doesn't have any post</p>";
			endif;
		echo $after_widget;
	}

	/** @see WP_Widget::update */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['cat_id'] = strip_tags($new_instance['cat_id']);
		$instance['num_post'] = strip_tags($new_instance['num_post']);
		$instance['num_post_with_content'] = strip_tags($new_instance['num_post_with_content']);
		$instance['content_excerpt'] = strip_tags($new_instance['content_excerpt']);
		return $instance;
	}

	/** @see WP_Widget::form */
	function form( $instance ) {
		if ( $instance ) {
			$title = esc_attr( $instance[ 'title' ] );
			$cat_id = esc_attr( $instance[ 'cat_id' ] );
			$num_post = esc_attr( $instance[ 'num_post' ] );
			$num_post_with_content = esc_attr( $instance[ 'num_post_with_content' ] );
			$content_excerpt = esc_attr( $instance[ 'content_excerpt' ] );
		}
		else {
			$title = __( 'Sidebar Query', 'text_domain' );
			$cat_id = 1;
			$num_post = 5;
			$num_post_with_content = 1;
			$content_excerpt = 2;
		}
		?>
		<p>
		<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
		</p>
		<p>
		<label for="<?php echo $this->get_field_id('cat_id'); ?>"><?php _e('Category id:'); ?></label> 
		<?php wp_dropdown_categories('show_option_all=All categories&name='.$this->get_field_name('cat_id').'&selected='.$cat_id); ?>
		</p>
		<p>
		<label for="<?php echo $this->get_field_id('num_post'); ?>"><?php _e('Number posts:'); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id('num_post'); ?>" name="<?php echo $this->get_field_name('num_post'); ?>" type="text" value="<?php echo $num_post; ?>" />
		</p>
		<p>
		<label for="<?php echo $this->get_field_id('num_post_with_content'); ?>"><?php _e('Number posts with content:'); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id('num_post_with_content'); ?>" name="<?php echo $this->get_field_name('num_post_with_content'); ?>" type="text" value="<?php echo $num_post_with_content; ?>" />
		</p>
		<p>
		<label for="<?php echo $this->get_field_id('content_excerpt'); ?>"><?php _e('Display Content or Excerpt?'); ?></label>
		<select name="<?php echo $this->get_field_name('content_excerpt'); ?>" id="<?php echo $this->get_field_id('content_excerpt'); ?>" class="widefat">
			<option value="1" <?php if ( '1' == $instance['content_excerpt'] ) echo 'selected="selected"'; ?>>Content</option>
			<option value="2" <?php if ( '2' == $instance['content_excerpt'] ) echo 'selected="selected"'; ?>>Excerpt</option>
		</select>
		</p>
		<?php 
	}
	/** @see WP_Widget::Change Currency */
	

} // class yStore_Query_Widget

// register yStore_Query_Widget widget
add_action( 'widgets_init', create_function( '', 'register_widget("yStore_Query_Widget");' ) );
function jigoshop_add_my_currency( $currencies ) {
     $currencies['VND'] = 'VietNamDong (VND)';
     asort($currencies);
     return $currencies;
}
add_filter('jigoshop_currencies', 'jigoshop_add_my_currency', 1, 1);
function jigoshop_add_my_currency_symbol( $currency_symbol, $currency ) {
    if ($currency=='VND') $currency_symbol = 'VND';
    return $currency_symbol;
}
add_filter('jigoshop_currency_symbol', 'jigoshop_add_my_currency_symbol', 1, 2);
?>